//
//  ImageGrayscaleTransparency.metal
//  AITattooApp
//
//  Created by LWT on 2024/11/19.
//

#include <metal_stdlib>
#include "OperationShaderTypes.h"
using namespace metal;

typedef struct
{
    float transparencyEndValue;
    float transparencyStartValue;
    float transparencyRangeValue;
} ImageGrayscaleTransparencyFragmentUniform;

fragment half4 imageGrayscaleTransparencyFragment(SingleInputVertexIO fragmentInput [[stage_in]], texture2d<half> inputTexture [[texture(0)]], constant ImageGrayscaleTransparencyFragmentUniform& uniform [[buffer(1)]])
{
    constexpr sampler quadSampler;
    half4 color = inputTexture.sample(quadSampler, fragmentInput.textureCoordinate);
    half intensity = (color.r+color.g+color.b)/3.0;
    half alpha;
    if (intensity >= uniform.transparencyEndValue) {
        alpha = 0.0h;
    } else {
        if (intensity > uniform.transparencyStartValue) {
            alpha = (uniform.transparencyEndValue-intensity)/uniform.transparencyRangeValue;
        } else {
            alpha = 1.0h;
        }
    }
    return half4(color.r, color.g, color.b, alpha);
}
